package com.latinia.limsp.test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.jms.JMSException;
import javax.xml.rpc.ServiceException;

import com.latinia.limsp.db.MsgPersistence;
import com.latinia.limsp.util.formatter.client.FormatterFactory;
import com.latinia.limsp.util.formatter.client.FormatterMessage;

public class SendINOT  extends SendMessage
{
	Destination destination;
	FormatterMessage inotFormatter;
	XMLManager manager;
	Contract contract;
    public SendINOT(XMLManager manager, Contract contract) {
    	super(contract.getProperties());        	
    	this.manager=manager;
    	this.contract=contract;
	} 
	public void sendMessageJMS(int i,MsgPersistence msgPersistence)throws JMSException, Exception
    {
		inotFormatter.setContent(destination.getMessage()+" JMS "+i);
        sendByJMS(inotFormatter.toString());
        System.out.println("INOT "+i+" enviado por JMS");
        msgPersistence.insert(contract.getTest_case(), inotFormatter.getID(),inotFormatter.toString());
    }
    public void sendMessageWS(int i,MsgPersistence msgPersistence)throws MalformedURLException,ServiceException,RemoteException,Exception
    {               	
    	inotFormatter.setContent(destination.getMessage()+" WS "+i);
        sendByWS(inotFormatter.toString());
        System.out.println("INOT "+i+" enviado por JMS");
        msgPersistence.insert(contract.getTest_case(), inotFormatter.getID(),inotFormatter.toString());
    }
    public void createXML()
    {
    	destination=contract.getDestination();
    	destination.setMessage(contract.getMessageInot());
    	inotFormatter=FormatterFactory.newFormatterMessageOut("inot", "inot");
    	if (contract.getId()!=null) inotFormatter.setIdContract(contract.getId());
    	else if (contract.getRef() != null) {
    		inotFormatter.setLoginEnterprise(contract.getCompany());
    		inotFormatter.setRefContract(contract.getRef());
        }    	    		    	
    	for(Destination d: contract.getDestinations()){
    		if(d.getEmails()!=null||d.getEmailsCC()!=null||d.getEmailsBCC()!=null){
    			if(d.getEmails()!=null)
    			for(String email:d.getEmails())        
    				inotFormatter.addAddressGeneric("to","email",email);        
    	        if(d.getEmailsCC()!=null)
    	        for(String email:d.getEmailsCC())
    	        	inotFormatter.addAddressGeneric("cc","email", email);
    	        if(d.getEmailsBCC()!=null)
    	        for(String email:d.getEmailsBCC())
    	        	inotFormatter.addAddressGeneric("bcc","email", email);
    		}else if(d.getKey()!=null||d.getValue()!=null){
    			if(d.getKey()!=null){
    				if(!d.getKey().equals("#ref_user")){
    					inotFormatter.setCustomerKeyName(d.getKey());
    				}
    			}
    			inotFormatter.setCustomerKeyValue(d.getValue());
    		}
    		else{
    			inotFormatter.addAddressGeneric("to", "phone", d.getTo());
    		}
    	}    	
    	if(contract.getChannels()!=null){
    		inotFormatter.setDeliveryChannels(contract.getChannelsArray());
    		inotFormatter.setMaxDeliveryChannels(contract.getMaxChannels());
    	}
    }
}
